<?php

class ActivityTaskService
{
    /**
     * @var ActivityTaskService
     */
    private static $instance;

    /**
     * Get the instance of ActivityTaskService.
     *
     * @return ActivityTaskService
     */
    public static function get()
    {
        if (!self::$instance) {
            self::$instance = new ActivityTaskService();
        }
        return self::$instance;
    }

    /**
     * Construction.
     */
    protected function __construct()
    {
    }

    /**
     * 处理用户创建时的逻辑
     *
     * @param int $shardId
     * @return void
     */
    public function handleCreate($shardId)
    {

    }

    /**
     * 处理用户登录时的逻辑
     *
     * @param int $userId
     * @return void
     */
    public function handleLogin($userId)
    {
        // do nothing
    }

    /**
     * 获取玩家活动任务
     * @param $userId
     * @param $activityId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function getActivityTaskInfo($userId, $activityId, $reqTime)
    {
        $taskData = array();
        $hasTask = false;
        $taskDataList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_TASK);
        $taskDatas = $taskDataList->toArray();


        foreach ($taskDatas as $tid=>$data){
            if ($activityId == $data['activityId']){
                $hasTask = true;
                $taskData[] = array(
                    'taskId'    => $data['taskType'],
                    'position'  => $data['position'],
                    'quality'   => $data['quality'],
                    'progress'  => $data['progress'],
                    'isReward'  => $data['isReward']
                );
            }
        }

        if (!$hasTask){
            //随机任务
            $taskId = ActivityService::get()->randTaskId();
            $quality = ActivityService::get()->randWeightQuality();
            //生成一档任务
            $activityTaskData = $taskDataList->getElement(1,false);
            if (!$activityTaskData){
                $activityTaskData = new ActivityTaskVo(
                    $userId, // userId
                    1,//position
                    $taskId, // taskId
                    $activityId, //activityId
                    $quality, // quality
                    0, // progress
                    0,// isReward
                    $reqTime,// $createTime
                    true
                );
            }

            if ($activityTaskData->isInsert() && !SzUtility::checkArrayKey(1, $taskDataList->getList())) {
                $taskDataList->addElement($activityTaskData);
                $taskDataList->setElement(1,$activityTaskData);
            }

            $taskData[] = array(
                'taskId'    => $taskId,
                'position'  => 1,
                'quality'   => $quality,
                'progress'  => 0,
                'isReward'     => 0
            );
        }

        SzPersister::get()->setVoList($taskDataList);

        return $taskData;
    }

    /**
     * 刷新活动任务
     * @param $userId
     * @param $activityId
     * @param $position
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function flushActivityTask($userId, $activityId, $position,$reqTime)
    {
        /**
         * @var ActivityTaskVo $taskData
         */
        //验证活动是否开启
        $activityConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, $activityId, true);
        //获取玩家平台数据
        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        $platformPre = GameConst::PLATFORM_TIME_ARRAY[$platform];
        $startTime = strtotime($activityConf[$platformPre.'TriggerTime']);
        $endTime = strtotime($activityConf[$platformPre.'EndTime']);

        /**
         * 获取目标玩家数据
         * @var $activityCommonDataList ActivityCommonDataVoList
         * @var $activityCommonData ActivityCommonDataVo
         */
        $activityCommonDataList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_COMMON);
        $activityCommonData = $activityCommonDataList->getElement($activityId,false);
        $mineStartTime = $activityCommonData ? $activityCommonData->getCreateTime() : $startTime;
        $mineEndTime = $activityCommonData ? $activityCommonData->getEndTime() : $endTime;

        if ($mineStartTime > $reqTime || $mineEndTime <= $reqTime) {
            throw new SzException(90001);//活动没有开始
        }

        //获取玩家任务数据
        $taskDataList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_TASK);
        $taskData = $taskDataList->getElement($position);
        if (!$taskData){
            throw new SzException(90071,$position);//请购买任务档位
        }

        //任务随机
        $hasTask = $taskDataList->toArray();
        $constTaskTypeArr = array(GameConst::ACTIVITY_TASK_1,GameConst::ACTIVITY_TASK_2,GameConst::ACTIVITY_TASK_3,GameConst::ACTIVITY_TASK_4,GameConst::ACTIVITY_TASK_5);
        foreach ($hasTask as $p=>$data){
            if (in_array($data['taskType'],$constTaskTypeArr)){
                unset($constTaskTypeArr[$data['taskType'] - 1]);
            }
        }
        $taskKey = array_rand($constTaskTypeArr);
        $newTaskId = $constTaskTypeArr[$taskKey];
        if (empty($newTaskId)){
            $newTaskId = 1;
        }

        //品质权重随机
        $quality = ActivityService::get()->randWeightQuality();

        //消耗道具，策划决定固定值2钻石
        $use = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'activityTaskRefresh', true);
        GameService::get()->handleEntity($userId,GameConst::ENTITY_ACTION_CONSUME_MONEY, $use['value'], true);

        //任务数据更新
        $taskData->setTaskType($newTaskId);
        $taskData->setQuality($quality);
        $taskData->setProgress(0);
        $taskData->setIsReward(0);
        $taskData->setCreateTime($reqTime);

        $taskDataList->updateElement($position,$taskData);
        SzPersister::get()->setVoList($taskDataList);

        return array(
            'position' => $position,//任务档位
            'taskId' => $newTaskId,//任务ID
            'activityId' => $activityId, //活动ID
            'quality' => $quality,//任务品质
            'progress' => 0,//任务进度
            'isReward' => 0,//完成情况【0：未完成，1：完成可领奖，2：已经领奖】
            'time'=>$reqTime,//更新时间
        );
    }

    /**
     * 跳过活动任务
     * @param $userId
     * @param $activityId
     * @param $position
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function jumpActivityTask($userId, $activityId, $position,$reqTime)
    {
        /**
         * @var ActivityTaskVo $taskData
         */
        //验证活动是否开启
        $activityConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, $activityId, true);
        //获取玩家平台数据
        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        $platformPre = GameConst::PLATFORM_TIME_ARRAY[$platform];
        $startTime = strtotime($activityConf[$platformPre.'TriggerTime']);
        $endTime = strtotime($activityConf[$platformPre.'EndTime']);

        /**
         * 获取目标玩家数据
         * @var $activityCommonDataList ActivityCommonDataVoList
         * @var $activityCommonData ActivityCommonDataVo
         */
        $activityCommonDataList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_COMMON);
        $activityCommonData = $activityCommonDataList->getElement($activityId,false);
        $mineStartTime = $activityCommonData ? $activityCommonData->getCreateTime() : $startTime;
        $mineEndTime = $activityCommonData ? $activityCommonData->getEndTime() : $endTime;

        if ($mineStartTime > $reqTime || $mineEndTime <= $reqTime) {
            throw new SzException(90001);//活动没有开始
        }

        //获取玩家任务数据
        $taskDataList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_TASK);
        $taskData = $taskDataList->getElement($position);
        if (!$taskData){
            throw new SzException(90071,$position);//请购买任务档位
        }
        $taskId = $taskData->getTaskType();

        //获取任务配置
        $taskConfs = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_ACTIVITY_TASK, null, true);
        $taskConf = array();
        foreach ($taskConfs as $tid=>$data){
            if ($tid == $taskId){
                $taskConf = $data;
            }
        }
        if (empty($taskConf)){
            throw new SzException(90072,$taskId);//活动任务配置不存在
        }

        //获取任务品质配置
        $quality = $taskData->getQuality();
        $qualityConfs = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_ACTIVITY_TASK_QUALITY, null, true);
        $qualityConf = array();
        foreach ($qualityConfs as $q=>$data){
            if ($q == $quality){
                $qualityConf = $data;
            }
        }
        if (empty($qualityConf)){
            throw new SzException(90073,$quality);//活动任务品质配置不存在
        }

        //消耗道具(两次向上取整)
        $nowProgress = $taskData->getProgress();
        $price = ceil($qualityConf['jumpprice'] * (1 - $nowProgress/$taskConf['progress']));
        GameService::get()->handleEntity($userId,GameConst::ENTITY_ACTION_CONSUME_MONEY, $price, true);

        //任务数据更新
        $taskData->setProgress($taskConf['progress']);
        $taskData->setIsReward(1);

        $taskDataList->updateElement($position,$taskData);
        SzPersister::get()->setVoList($taskDataList);

        return array(
            'position' => $position,//任务档位
            'taskId' => $taskId,//任务ID
            'activityId' => $activityId, //活动ID
            'quality' => $quality,//任务品质
            'progress' => $taskConf['progress'],//任务进度
            'isReward' => 1,//完成情况【0：未完成，1：完成可领奖，2：已经领奖】
            'time'=>$reqTime,//更新时间
        );
    }

    /**
     * 购买活动任务档位
     * @param $userId
     * @param $activityId
     * @param $position
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function buyActivityTaskPosition($userId, $activityId, $position,$reqTime)
    {
        /**
         * @var ActivityTaskVo $taskData
         */
        //验证活动是否开启
        $activityConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, $activityId, true);
        //获取玩家平台数据
        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        $platformPre = GameConst::PLATFORM_TIME_ARRAY[$platform];
        $startTime = strtotime($activityConf[$platformPre.'TriggerTime']);
        $endTime = strtotime($activityConf[$platformPre.'EndTime']);

        /**
         * 获取目标玩家数据
         * @var $activityCommonDataList ActivityCommonDataVoList
         * @var $activityCommonData ActivityCommonDataVo
         */
        $activityCommonDataList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_COMMON);
        $activityCommonData = $activityCommonDataList->getElement($activityId,false);
        $mineStartTime = $activityCommonData ? $activityCommonData->getCreateTime() : $startTime;
        $mineEndTime = $activityCommonData ? $activityCommonData->getEndTime() : $endTime;

        if ($mineStartTime > $reqTime || $mineEndTime <= $reqTime) {
            throw new SzException(90001);//活动没有开始
        }

        //获取玩家任务数据
        $newTaskId = 1;
        $quality = 1;
        $taskDataList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_TASK);
        $taskData = $taskDataList->getElement($position,false);
        if ($taskData){
            throw new SzException(90075,$position);//已购买该任务档位
        }
        if ($position == 1){
            throw new SzException(90075,1);//已购买该任务档位
        }

        if (!$taskData){
            //任务随机
            $hasTask = $taskDataList->toArray();
            $constTaskTypeArr = array(GameConst::ACTIVITY_TASK_1,GameConst::ACTIVITY_TASK_2,GameConst::ACTIVITY_TASK_3,GameConst::ACTIVITY_TASK_4,GameConst::ACTIVITY_TASK_5);
            foreach ($hasTask as $p=>$data){
                if (in_array($data['taskType'],$constTaskTypeArr)){
                    unset($constTaskTypeArr[$data['taskType'] - 1]);
                }
            }
            $taskKey = array_rand($constTaskTypeArr);
            $newTaskId = $constTaskTypeArr[$taskKey];
            if (empty($newTaskId)){
                $newTaskId = 1;
            }

            //品质权重随机
            $quality = ActivityService::get()->randWeightQuality();

            $taskData = new ActivityTaskVo(
                $userId, // userId
                $position, //position
                $newTaskId, // newTaskId
                $activityId, // activityId
                $quality,//quality
                0,//progress
                0,//isReward
                $reqTime,//createTime
                true
            );
        }

        $priceConf = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'activityTaskBuy', true);
        $prices =json_decode($priceConf['value']);
        if ($position == 2){
            $price = $prices[0];
        }
        if ($position == 3){
            $price = $prices[1];
        }
        if (empty($price)){
            $price = 50;
        }

        //消耗道具，策划决定固定值50钻石
        GameService::get()->handleEntity($userId,GameConst::ENTITY_ACTION_CONSUME_MONEY, $price, true);

        //任务数据创建
        if ($taskData->isInsert() && !SzUtility::checkArrayKey($position, $taskDataList->getList())) {
            $taskDataList->addElement($taskData);
            $taskDataList->setElement($position,$taskData);
        }
        SzPersister::get()->setVoList($taskDataList);

        return array(
            'newTask' => array(
                'userId' => $userId, // userId
                'position' => $position, //position
                'taskId' => $newTaskId, // newTaskId
                'activityId' => $activityId, // activityId
                'quality' => $quality,//quality
                'progress' => 0,//progress
                'isReward' => 0,//isReward
                'time' => $reqTime,//createTime
            )
        );
    }

    /**
     * 完成任务获得积分
     * @param $userId
     * @param $activityId
     * @param $position
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function getActivityTaskReward($userId, $activityId, $position,$reqTime)
    {
        /**
         * @var ActivityCommonDataVo $activityCommonData
         * @var ActivityTaskVo $taskData
         * @var ActivityTaskVoList $taskDataList
         */
        //验证活动是否开启
        $activityConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, $activityId, true);
        //获取玩家平台数据
        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        $platformPre = GameConst::PLATFORM_TIME_ARRAY[$platform];
        $startTime = strtotime($activityConf[$platformPre.'TriggerTime']);
        $endTime = strtotime($activityConf[$platformPre.'EndTime']);

        /**
         * 获取目标玩家数据
         * @var $activityCommonDataList ActivityCommonDataVoList
         * @var $activityCommonData ActivityCommonDataVo
         */
        //获取玩家活动公共数据
        $activityCommonDataList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_COMMON);
        $activityCommonData = $activityCommonDataList->getElement($activityId,false);
        if (!$activityCommonData){
            throw new SzException(90001);//活动没有开始
        }
        $mineStartTime = $activityCommonData ? $activityCommonData->getCreateTime() : $startTime;
        $mineEndTime = $activityCommonData ? $activityCommonData->getEndTime() : $endTime;

        if ($mineStartTime > $reqTime || $mineEndTime <= $reqTime) {
            throw new SzException(90001);//活动没有开始
        }

        //获取玩家任务数据
        $taskDataList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_TASK);
        $taskData = $taskDataList->getElement($position);
        if (!$taskData){
            throw new SzException(90071,$position);//请购买任务档位
        }
        $taskId = $taskData->getTaskType();

        //获取任务配置
        $taskConfs = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_ACTIVITY_TASK, null, true);
        $taskConf = array();
        foreach ($taskConfs as $tid=>$data){
            if ($tid == $taskId){
                $taskConf = $data;
            }
        }
        if (empty($taskConf)){
            throw new SzException(90072,$taskId);//活动任务配置不存在
        }

        //获取任务品质配置
        $quality = $taskData->getQuality();
        $qualityConfs = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_ACTIVITY_TASK_QUALITY, null, true);
        $qualityConf = array();
        foreach ($qualityConfs as $q=>$data){
            if ($q == $quality){
                $qualityConf = $data;
            }
        }
        if (empty($qualityConf)){
            throw new SzException(90073,$quality);//活动任务品质配置不存在
        }

        //任务数据更新
        $progress = $taskData->getProgress();
        if ($progress < $taskConf['progress']){
            throw new SzException(90076);//该活动任务没有完成
        }

        if ($taskData->getIsReward() != 1){
            throw new SzException(90076);//该活动任务没有完成
        }

        //该档位刷新任务
        $hasTask = $taskDataList->toArray();
        $constTaskTypeArr = array(GameConst::ACTIVITY_TASK_1,GameConst::ACTIVITY_TASK_2,GameConst::ACTIVITY_TASK_3,GameConst::ACTIVITY_TASK_4,GameConst::ACTIVITY_TASK_5);
        foreach ($hasTask as $p=>$data){
            if (in_array($data['taskType'],$constTaskTypeArr)){
                unset($constTaskTypeArr[$data['taskType'] - 1]);
            }
        }
        $taskKey = array_rand($constTaskTypeArr);
        $newId = $constTaskTypeArr[$taskKey];
        if (empty($newId)){
            $newId = 1;
        }

        $newQuality = ActivityService::get()->randWeightQuality();

        $taskData->setTaskType($newId);
        $taskData->setQuality($newQuality);
        $taskData->setProgress(0);
        $taskData->setIsReward(0);
        $taskData->setCreateTime($reqTime);

        $newTask = array(
            'position' => $position,//任务档位
            'taskId' => $newId,//任务ID
            'activityId' => $activityId, //活动ID
            'quality' => $newQuality,//任务品质
            'progress' => 0,//任务进度
            'isReward' => 0,//完成情况【0：未完成，1：完成可领奖，2：已经领奖】
            'time'=>$reqTime,//更新时间
        );
        $taskDataList->updateElement($position,$taskData);
        SzPersister::get()->setVoList($taskDataList);

        $activityCommonData->setTotalPoint($activityCommonData->getTotalPoint() + $qualityConf['reward']);
        $activityCommonDataList->updateElement($activityId,$activityCommonData);
        SzPersister::get()->setVoList($activityCommonDataList);

        return array(
            'total' => $activityCommonData->getTotalPoint(),
            'surplus' => $activityCommonData->getTotalPoint() - $activityCommonData->getUsePoint(),
            'newTask' => $newTask
        );
    }

    /**
     * 积分购买礼包
     * @param $userId
     * @param $activityId
     * @param $giftId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function usePointBuyActivityTaskChest($userId, $activityId, $giftId,$reqTime)
    {
        /**
         * @var ActivityCommonDataVo $activityCommonData
         * @var ActivityTaskVo $taskData
         */
        //验证活动是否开启
        $activityConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, $activityId, true);
//        $serverTime = GameService::get()->getReqTime();
        //获取玩家平台数据
        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        $platformPre = GameConst::PLATFORM_TIME_ARRAY[$platform];
        $startTime = strtotime($activityConf[$platformPre.'TriggerTime']);
        $endTime = strtotime($activityConf[$platformPre.'EndTime']);

        /**
         * 获取目标玩家数据
         * @var $activityCommonDataList ActivityCommonDataVoList
         * @var $activityCommonData ActivityCommonDataVo
         */
        //获取玩家活动公共数据
        $activityCommonDataList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_COMMON);
        $activityCommonData = $activityCommonDataList->getElement($activityId,false);
        if (!$activityCommonData){
            throw new SzException(90001);//活动没有开始
        }
        $mineStartTime = $activityCommonData ? $activityCommonData->getCreateTime() : $startTime;
        $mineEndTime = $activityCommonData ? $activityCommonData->getEndTime() : $endTime;

        if ($mineStartTime > $reqTime || $mineEndTime <= $reqTime) {
            throw new SzException(90001);//活动没有开始
        }

        //获取宝箱购买配置
        $giftReward = (SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_ACTIVITY_TASK_CHEST, null, true))[$giftId];
        if (empty($giftReward)){
            throw new SzException(90079,$giftId);//宝箱配置不存在
        }

        $usePoint = $activityCommonData->getUsePoint();
        $hasPoint = $activityCommonData->getTotalPoint() - $usePoint;

        if (!$activityCommonData){
            throw new SzException(90001);//活动没有开始
        }

        $useConf = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'activityTaskChestBuy', true);
        if ($giftId == 1){
            $useActivityItem = $useConf['value'][0];
        }
        if ($giftId == 2){
            $useActivityItem = $useConf['value'][1];
        }
        if ($giftId == 3){
            $useActivityItem = $useConf['value'][2];
        }
        if (empty($useActivityItem)){
            $useActivityItem = 50;
        }

        if ($hasPoint < $useActivityItem){
            throw new SzException(90078);//活动道具不足
        }

        //获得宝箱奖励
        $itemId = array_keys($giftReward['itemNum'])[0];
        $funcNum = $giftReward['itemNum'][$itemId];
        //获取暴击倍率
        $weight = 0;
        $temp = array();
        foreach ($giftReward['weight'] as $key=>$data){
            $weight += $data;
            for ($i=0;$i<$data;$i++){
                $temp[] = $key;
            }
        }

        $magnification = rand(0, $weight-1);
        $constMagnification = array(1,2,3,5);
        $num = $constMagnification[$temp[$magnification]]*$funcNum;
        //宝箱物品获得
        GameService::get()->handleEntity($userId,$itemId, $num, true);

        $activityCommonData->setUsePoint($usePoint + 50);

        $activityCommonDataList->updateElement($activityId,$activityCommonData);
        SzPersister::get()->setVoList($activityCommonDataList);

        return array(
            'magnification' => $constMagnification[$temp[$magnification]],
            'itemId' => $itemId,
            'num' => $num
        );
    }

    /**
     * 兑换活动目标奖励
     * @param $userId
     * @param $activityId
     * @param $step
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function exchangeActivityTaskReward($userId, $activityId, $step,$reqTime)
    {
        /**
         * @var ActivityCommonDataVo $activityCommonData
         * @var ActivityTaskVo $taskData
         */
        //验证活动是否开启
        $activityConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, $activityId, true);
        //获取玩家平台数据
        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        $platformPre = GameConst::PLATFORM_TIME_ARRAY[$platform];
        $startTime = strtotime($activityConf[$platformPre.'TriggerTime']);
        $endTime = strtotime($activityConf[$platformPre.'EndTime']);

        /**
         * 获取目标玩家数据
         * @var $activityCommonDataList ActivityCommonDataVoList
         * @var $activityCommonData ActivityCommonDataVo
         */
        //获取玩家活动公共数据
        $activityCommonDataList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_COMMON);
        $activityCommonData = $activityCommonDataList->getElement($activityId,false);
        if (!$activityCommonData){
            throw new SzException(90001);//活动没有开始
        }
        $mineStartTime = $activityCommonData ? $activityCommonData->getCreateTime() : $startTime;
        $mineEndTime = $activityCommonData ? $activityCommonData->getEndTime() : $endTime;

        if ($mineStartTime > $reqTime || $mineEndTime <= $reqTime) {
            throw new SzException(90001);//活动没有开始
        }

        //获取阶段奖励兑换配置
        $stepReward = (SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_ACTIVITY_TASK_REWARD, null, true))[$step];
        if (empty($stepReward)){
            throw new SzException(90077,$step);//活动目标配置不存在
        }

        $totalPoint = $activityCommonData->getTotalPoint();
        $usePoint = $activityCommonData->getUsePoint();
        $getReward = $activityCommonData->getRewardInfo();
        if (!$activityCommonData){
            throw new SzException(90001);//活动没有开始
        }

        if ($stepReward['number'] > $totalPoint){
            throw new SzException(90078);//活动道具不足
        }

        $reward = array();
        if (is_null($getReward) || empty($getReward) || $getReward == 'null' || $getReward == '' || $getReward == null){
            $getReward = $step;
            $reward[] = $step;
            $getRewardArr[] = $step;
        }
        else{
            $getReward = str_replace("null:", "", $getReward);
            $getReward = str_replace("\"", "", $getReward);
            $getRewardArr = explode(':',$getReward);
            if (in_array($step,$getRewardArr)){
                throw new SzException(90007);//活动没有开始
            }
            $getRewardArr[] = $step;
            $reward = $getRewardArr;
        }

        //获得阶段奖励
        $items = $stepReward['reward'];
        foreach ($items as $key=>$num){
            GameService::get()->handleEntity($userId, $key, $num, true);
        }
        $activityCommonData->setRewardInfo(implode(':',$reward));

        $activityCommonDataList->updateElement($activityId,$activityCommonData);
        SzPersister::get()->setVoList($activityCommonDataList);

        return array(
            'getReward' => $reward
        );
    }

    /**
     * 进度更新
     * @param $userId
     * @param $activityId
     * @param $position
     * @return bool|void
     * @throws SzException
     */
    public function updateActivityTaskProgress($userId, $activityId, $position,$count=1)
    {
        /**
         * @var ActivityCommonDataVo $activityCommonData
         * @var ActivityTaskVo $taskData
         */
        //验证活动是否开启
        $activityConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, $activityId, true);
        $serverTime = GameService::get()->getReqTime();
        $startTime = $activityConf['startTime'];
        $endTime = $activityConf['endTime'];

        /**
         * 获取目标玩家数据
         * @var $activityCommonDataList ActivityCommonDataVoList
         * @var $activityCommonData ActivityCommonDataVo
         */
        //获取玩家活动公共数据
        $activityCommonDataList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_COMMON);
        $activityCommonData = $activityCommonDataList->getElement($activityId,false);
        if (!$activityCommonData){
            throw new SzException(90001);//活动没有开始
        }
        $mineStartTime = $activityCommonData ? $activityCommonData->getCreateTime() : $startTime;
        $mineEndTime = $activityCommonData ? $activityCommonData->getEndTime() : $endTime;

        if ($mineStartTime > $serverTime || $mineEndTime <= $serverTime) {
            return;
        }

        //获取玩家任务数据
        $taskDataList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_TASK);
        $taskData = $taskDataList->getElement($position);
        if (!$taskData){
            return;
        }
        $taskId = $taskData->getTaskType();

        //获取任务配置
        $taskConfs = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_ACTIVITY_TASK, null, true);
        $taskConf = array();
        foreach ($taskConfs as $tid=>$data){
            if ($tid == $taskId){
                $taskConf = $data;
            }
        }
        if (empty($taskConf)){
            return;
        }

        //任务数据更新
        if ($taskData->getIsReward() == 1 || $taskData->getIsReward() == 2){
            return;
        }

        $progress = $taskData->getProgress() + $count;
        if ($progress >= $taskConf['progress']){
            $taskData->setProgress($taskConf['progress']);
            $taskData->setIsReward(1);//可领奖
        }
        else{
            $taskData->setProgress($progress);
        }
        $taskDataList->updateElement($position,$taskData);
        SzPersister::get()->setVoList($taskDataList);
    }
}